
<?php
class Medicine extends ORDataObject{
	var $medicine_id = '';
	var $name = '';
	var $note = '';
	var $default_usage_id = '';
	var $original_name_id = '';
	var $deleted = '';

	var $_table = 'medicine';
	var $_key = 'medicine_id';
	var $_internalName = 'Medicine';

	function Medicine(){
		parent::ORDataObject();
	}

	function checkForExistingMedicineName($medicine_name){
	    $locale = L10nHelper::getLocale();	    
        $fieldUsageUnit = L10nHelper::changeField($locale, 'usage_unit');
        $fieldUsageTitle = L10nHelper::changeField($locale, 'usage_title');
        $fieldQuantityTitle = L10nHelper::changeField($locale, 'quantity_title');
	    
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "m.medicine_id, mdu.$fieldUsageUnit usage_unit, mdu.$fieldUsageTitle usage_title, 
			         mdu.usage_num, mdu.dosage, mdu.time, mdu.$fieldQuantityTitle quantity_title, 
			         mdu.quantity_num, mp.price, mp.insurance_support",
			'from' 		=> "medicine m 
			                LEFT JOIN medicine_default_usage mdu ON m.default_usage_id = mdu.id
							LEFT JOIN medicine_price mp ON mp.medicine_id = m.medicine_id														",
			'where'		=> "m.name = '$medicine_name' AND m.deleted = 0"
			);

		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (empty($result))	return false;
		return $result[0];
	}

	function getMedicineSuggestionNames($q){	    				
		$sql = "SELECT name 
		      FROM medicine 
		      WHERE name LIKE '$q%' 
		          AND deleted = 0 
		      LIMIT 20";

		$rows = $this->_db->GetAll($sql);
		$result = array();
		for ($i = 0; $i < sizeof($rows); $i++){
			$result[$i] = $rows[$i]['name'];
		}
		return $result;
	}


	/**
	 * Check if the medicine name is existed.
	 *
	 * @param String $medicine_name
	 * @return false if the medicine name is not existed.
	 */
    function isExistedMedicineName($medicine_name){          
        $ds = new Datasource_sql();
        $sql = array(
            'cols'      => "medicine_id",
            'from'      => "$this->_table", 
            'where'     => " name='$medicine_name'"
            );

        $ds->setup($this->_db,$sql);
        $result = $ds->toArray();
        if (empty($result)) return false;
        return $result[0];
    }
    /**
	 * @author hdviet
	 * update for localize others
	 */
    /**
     * Get all medicine filtered by the specific name.
     *
     * @param String $name
     * @return List of medicine
     */
	// TODO localization
    function getMedicineListByName($name, $status) {
        $fieldUsageTitle = L10nHelper::changeField($locale, 'usage_title');
        
    	
    	$whereCond = '1';
    	if (isset($name) && $name != "") {
    		$whereCond .= " and m.name LIKE '%$name%'";
    	}
    	if(isset($status) && $status != 'all') {
    		$whereCond .= " and medicine.deleted = $status";
    	}

    	$ds = new Datasource_sql();
    	//localize  medicine.name, mdu.usage_title, mdu.quantity_title, mp.price
        $sql = array(
            'cols'      => "medicine.medicine_id, medicine.name, medicine.note, medicine.deleted, mon.name as originalName,
                            mdu.usage_unit, mdu.$field as usage_title, mdu.usage_num, mdu.dosage, mdu.time, mp.price, mp.selling_price, mp.insurance_support,
                            mdu.quantity_title, mdu.quantity_num ",
            'from'      => "medicine 
                            LEFT JOIN medicine_default_usage mdu ON (medicine.default_usage_id = mdu.id)
                            LEFT JOIN medicine_original_name mon ON (medicine.original_name_id = mon.id)
                            LEFT JOIN medicine_price mp ON (medicine.medicine_id = mp.medicine_id)                            
                            ",
            'where'     => $whereCond,
            'orderby'   => "medicine.name"
            );
        $ds->setup($this->_db,$sql);

        return $ds->toArray();
    }
    
    /**
     * Get list of medicine by functional type.
     * @return array of medicine
     */
    public function getMedicineListByType($medTypeId) {
                        
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "distinct medicine_id, name",
            'from'    => $this->_table,
            'where'   => " functional_type = '" . $medTypeId . "'",
            'orderby' => " name");
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }
    
    /**
     * Get quantity title of a specific medicine
     * @param $medicineId The id of medicine
     */
    public function getQuantityTitleOfMedicine($medicineId) {        
    	$field	=	L10nHelper::changeField(L10nHelper::getLocale(), "quantity_title");
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "mdu.$field as name",
            'from'    => $this->_table . " m"
                         . " INNER JOIN medicine_default_usage mdu ON m.default_usage_id = mdu.id ",
            'where'   => " m.medicine_id = '" . $medicineId . "'");
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
        	return false;
        } else {
        	return $result[0];
        }        
    }
}


?>